package cn.com.fuhuitong.main.goods.ui.activity

import androidx.lifecycle.Observer
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.cate.entity.CateChildEntity
import cn.com.fuhuitong.main.goods.adpter.GVT
import cn.com.fuhuitong.main.goods.adpter.GoodsListAdapter
import cn.com.fuhuitong.main.goods.vm.GoodsViewModel
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import cn.com.fuhuitong.R
import kotlinx.android.synthetic.main.activity_goods_list.*

@Route(path = ARouteConstance.ACTIVITY_URL_GOODS_LIST)
class GoodsListActivity : ViewModeActivity<GoodsViewModel>() {


    private lateinit var cateChildEntity: CateChildEntity

    override fun viewModel(): GoodsViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_goods_list

    override fun initView() {

        cateChildEntity = intent.getSerializableExtra(AppConstance.BUNDLE_CATE_CHILD)
                as CateChildEntity
        text_goods_title.text = cateChildEntity.name

        image_goods_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(recycler_goods_list, arrayOf(GVT.GOODS_LIST.ordinal))

        delegateAdapter.addAdapter(initViewGoodsListAdapter().apply {
            goodsListAdapter = this
        })
        goodsListAdapter.setOnItemClickListener { _, _, goodsListEntity ->
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_GOODS_DETAILS)
                .withInt(AppConstance.BUNDLE_GOODS_ID, goodsListEntity.id)
                .navigation()
        }

        swipe_refresh_goods.setOnRefreshListener {
            viewModel.goodsList(cateChildEntity.id, cateChildEntity.search)
        }
    }

    override fun initViewMode() {
        viewModel.goodsListDataLiveData.observe(this, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = response.response?.data?.size ?: 0, swipe = swipe_refresh_goods,
                emptyContent = EmptyContent("去逛逛", "暂无数据")
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                if (it.size == 0)
                    return@let
                goodsListAdapter.setAddDataNew(it)
            }
        })
    }

    override fun initData() {
        viewModel.goodsList(cateChildEntity.id, cateChildEntity.search)
    }

    override fun onLoadingEmptyView(state: EmptyState) {
        super.onLoadingEmptyView(state)
        if (state == EmptyState.FAILURE) {
            viewModel.goodsList(cateChildEntity.id, cateChildEntity.search)
        } else if (state == EmptyState.NULL) {
            this.finish()
        }
    }

    /**
     * 初始化物品列表适配器
     * */
    private lateinit var goodsListAdapter: GoodsListAdapter
    private fun initViewGoodsListAdapter(): GoodsListAdapter {
        val helper = LinearLayoutHelper()
        helper.setDividerHeight(
            resources.getDimensionPixelSize(R.dimen.dp_10)
        )
        return GoodsListAdapter(this, helper)
    }
}